package com.c.ctgproject.ui.collect

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.adapter.ImageCommonRvV2Adapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityBatteryHandlerOverBinding
import com.c.ctgproject.entity.guang.BatteryHandlerEntityV3
import com.c.ctgproject.entity.guang.PowerBatteryInfoEntity
import com.c.ctgproject.event.PositionEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import com.lwkandroid.imagepicker.ImagePicker
import com.lwkandroid.imagepicker.data.ImageBean
import com.lwkandroid.widget.ninegridview.NineGridBean
import org.greenrobot.eventbus.EventBus
import java.io.File

/**
 *作者:LiBW
 *创建日期: 2021/9/10
 *描述: 拆解完成 页面 动力电池拆解 页面 完成拆解 页面
 */
class BatteryHandlerOverActivity : BaseActivityV2() {

    private val mBinding by lazy { ActivityBatteryHandlerOverBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private val data by lazy { intent.getSerializableExtra(MyConstants.DATA) as BatteryHandlerEntityV3.DataBean.ListBean }
    private val imageShowList = ArrayList<String>()
    private val imageResultCode = 100
    private val mListPosition by lazy { intent.getIntExtra(MyConstants.POSITION,-1) }

    override fun event() {
        mBinding.mPhotoNGV.init(this, imageShowList, 3, imageResultCode)
        mBinding.stockName.textSet(data.batteryName.handlerNull())
        mBinding.batteryCode.textSet(data.batteryCode.handlerNull())
        mBinding.stockNum.textSet(data.count.handlerNull())
        mBinding.weight.textSet(data.weight.handlerNull())
        mBinding.voltage.textSet(data.voltage.handlerNull())
        mBinding.capacityKWh.textSet(data.capacityKwh.handlerNull())
        mBinding.mRemarkET.textSet(data.remark.handlerNull())
        mBinding.capacityAh.textSet(data.capacityAh.handlerNull())
        mBinding.moduleCount.textSet(data.moduleCount.handlerNull())
        mBinding.stockPosi.textSet(data.stockPosi.handlerNull())
        data.picUrls?.let { images ->
            //预处理
            val preHandlerList = ArrayList<String>()
            images.forEach { bean ->
                //图片类型 1-预处理 2-拆解毁型 3-入库
                if (bean.type == 1) {
                    preHandlerList.add(bean.url.handlerNull())
                }
            }
            if (preHandlerList.isEmpty()){
                mBinding.mOldImageTV.visibility= View.GONE
            }else{
                mBinding.mOldImageRV.adapter = ImageCommonRvV2Adapter(this, preHandlerList)
                mBinding.mOldImageRV.layoutManager = GridLayoutManager(this, 3, LinearLayoutManager.VERTICAL, false)
            }
//            val resultList: MutableList<NineGridBean> = ArrayList()
//            images.forEach { bean ->
//                val nineGirdData = NineGridBean(bean)
//                imageShowList.add(bean)
//                resultList.add(nineGirdData)
//            }
//            MyLog.d("旧照片数量:${imageShowList.size}")
//            mBinding.mPhotoNGV.addDataList(resultList)
        }
    }

    override fun click() {
        mBinding.voltage.handlerDecimal3 {
            val result = handlerKwh(it, mBinding.capacityAh.text.handlerNull())
            result?.let {
                mBinding.capacityKWh.textSet(result)
            }
        }
        mBinding.capacityAh.handlerDecimal3 {
            val result = handlerKwh(it, mBinding.voltage.text.handlerNull())
            result?.let {
                mBinding.capacityKWh.textSet(result)
            }
        }
        mBinding.capacityKWh.handlerDecimal3()


//        mBinding.mDCB.setOnClickListener { batteryType=1 }
//        mBinding.mMZ.setOnClickListener { batteryType=2 }
//        mBinding.mDX.setOnClickListener { batteryType=3 }


        mBinding.mConfirmBTN.setOnClickListener {
            when {
                mBinding.stockName.text.handlerNull().isEmpty() -> showToast("请输入电池名称")
//                mBinding.batteryCode.text.handlerNull().isEmpty() -> showToast("请输入电池编码")
                mBinding.stockNum.text.handlerNull().isEmpty() -> showToast("请输入电池数量")
//                mBinding.capacityKWh.text.handlerNull().isEmpty() -> showToast("请输入容量Kwh")
                mBinding.moduleCount.text.handlerNull().isEmpty() -> showToast("请输入模组数量")
                else -> {
                    if (imageShowList.isEmpty()) {
                        showToast("请上传照片")
                    } else {
                        val fileList = ArrayList<File>()
                        var index = 0

                        imageShowList.forEach {
                            index++
                            val file = File(it)
//                            val newFile = ImageUtil.imageWaterMark(this, file, index)
                            val newFile: File = WaterRemark.handlerWaterRemark2File(this, file, index.toString())
                            fileList.add(newFile)
                        }
                        upDataFile(fileList) {
                            val dataList = ArrayList<String>()
                            it?.forEach { b ->
                                dataList.add(b.fileUrl.handlerNull())
                            }
                            submitToHttp(dataList)
                        }
                    }

//                    //所有本地文件的图片
//                    val filePathList = ArrayList<String>()
//                    imageShowList.forEach {
//                        if (!it.startsWith("http")) {
//                            filePathList.add(it)
//                        }
//                    }
//
//                    if (filePathList.isNotEmpty()) {
//                        val fileList = ArrayList<File>()
//                        var index = 0
//                        filePathList.forEach {
//                            index++
//                            val file = File(it)
//                            //图片压缩
////                            val newFile: File = CompressHelper.getDefault(this).compressToFile(file)
//                            val newFile = ImageUtil.imageWaterMark(this, file, index)
//                            fileList.add(newFile)
//                        }
//                        upDataFile(fileList) {
//                            val dataList = ArrayList<String>()
//                            it?.forEach { b ->
//                                dataList.add(b.fileUrl.handlerNull())
//                            }
//                            MyLog.d("有新的文件")
//                            //旧的也给他
//                            imageShowList.forEach { bean ->
//                                if (bean.startsWith("http")) {
//                                    dataList.add(bean)
//                                }
//                            }
//                            submitToHttp(dataList)
//                        }
//                    } else {
//                        //没有新文件 但是有旧的url
//                        if (imageShowList.isNotEmpty()) {
//                            MyLog.d("没有新文件给他旧url")
//                            submitToHttp(imageShowList)
//                        } else {
//                            MyLog.d("啥都没有")
//                            submitToHttp(arrayListOf())
//                        }
//                    }
                }
            }


//            val moduleCode = mBinding.moduleCode.text.handlerNull()
//            val moduleCount = mBinding.moduleCount.text.handlerNull()
//            when {
//                moduleCode.isEmpty() -> showToast("请输入模组编码")
//                moduleCount.isEmpty() -> showToast("请输入模组数量")
//                else -> {
//                    ApiMethods.apiSubscribe(ApiMethods.retrofit.batteryHandlerOver(handlerBody(
//                        "pbDismantleId" to pbDismantleId,
//                        "pbId" to pbId,
//                        "moduleCode" to moduleCode,
//                        "moduleCount" to moduleCount,
//                    )), MyObserverHandlerLoading(this, loading) {
//                        MyLog.d("提交电池拆解回调:$it")
//                        showToast("拆解完成")
//                        finish()
//                    })
//                }
//            }
        }
    }

    private fun submitToHttp(imageList: ArrayList<String>) {
        val map = HashMap<String, Any?>()
        map["pbId"] = data.pbId
        map["pbDismantleId"] = data.pbDismantleId
        map["batteryName"] = mBinding.stockName.text.handlerNull()
        map["batteryCode"] = mBinding.batteryCode.text.handlerNull()
        map["moduleCount"] = mBinding.moduleCount.text.handlerNull()
//                    map["batteryPackCode"] = mBinding.batteryPackCode.text.handlerNull()
        map["count"] = mBinding.stockNum.text.handlerNull()
//                    map["moduleCode"] = mBinding.moduleCode.text.handlerNull()
        map["weight"] = mBinding.weight.text.handlerNull()
        map["capacityAh"] = mBinding.capacityAh.text.handlerNull()
        map["voltage"] = mBinding.voltage.text.handlerNull()
        map["capacityKwh"] = mBinding.capacityKWh.text.handlerNull()
        map["remark"] = mBinding.mRemarkET.text.handlerNull()
        map["stockPosi"]=mBinding.stockPosi.text.handlerNull()
        map["picUrls"] = imageList


        ApiMethods.apiSubscribe(ApiMethods.retrofit.powerBatteryHandlerFinishV2(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
            MyLog.d("拆解电池完成回调:$it")
            showToast("提交成功")
            EventBus.getDefault().post(PositionEvent(mListPosition))
            finish()
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        data?.let {
            when (requestCode) {
                //图片回调
                imageResultCode -> {
                    val list: List<ImageBean>? = data.getParcelableArrayListExtra(ImagePicker.INTENT_RESULT_DATA)
                    val resultList: MutableList<NineGridBean> = ArrayList()
                    list?.forEach { imageBean ->
                        imageBean?.let {
                            val nineGirdData = NineGridBean(imageBean.imagePath)
                            imageShowList.add(imageBean.imagePath)
                            resultList.add(nineGirdData)
                        }
                    }
                    mBinding.mPhotoNGV.addDataList(resultList)
                }
                else -> {

                }
            }
        }
    }
}